{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Longest Absolute File Path"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #stack #depth-first-search #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #栈 #深度优先搜索 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: lengthLongestPath"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #文件的最长绝对路径"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>假设有一个同时存储文件和目录的文件系统。下图展示了文件系统的一个示例：</p>\n",
    "\n",
    "<p><img alt=\"\" src=\"https://assets.leetcode.com/uploads/2020/08/28/mdir.jpg\" style=\"height: 142px; width: 300px;\" /></p>\n",
    "\n",
    "<p>这里将 <code>dir</code> 作为根目录中的唯一目录。<code>dir</code> 包含两个子目录 <code>subdir1</code> 和 <code>subdir2</code> 。<code>subdir1</code> 包含文件 <code>file1.ext</code> 和子目录 <code>subsubdir1</code>；<code>subdir2</code> 包含子目录 <code>subsubdir2</code>，该子目录下包含文件 <code>file2.ext</code> 。</p>\n",
    "\n",
    "<p>在文本格式中，如下所示(⟶表示制表符)：</p>\n",
    "\n",
    "<pre>\n",
    "dir\n",
    "⟶ subdir1\n",
    "⟶ ⟶ file1.ext\n",
    "⟶ ⟶ subsubdir1\n",
    "⟶ subdir2\n",
    "⟶ ⟶ subsubdir2\n",
    "⟶ ⟶ ⟶ file2.ext\n",
    "</pre>\n",
    "\n",
    "<p>如果是代码表示，上面的文件系统可以写为 <code>\"dir\\n\\tsubdir1\\n\\t\\tfile1.ext\\n\\t\\tsubsubdir1\\n\\tsubdir2\\n\\t\\tsubsubdir2\\n\\t\\t\\tfile2.ext\"</code> 。<code>'\\n'</code> 和 <code>'\\t'</code> 分别是换行符和制表符。</p>\n",
    "\n",
    "<p>文件系统中的每个文件和文件夹都有一个唯一的 <strong>绝对路径</strong> ，即必须打开才能到达文件/目录所在位置的目录顺序，所有路径用 <code>'/'</code> 连接。上面例子中，指向 <code>file2.ext</code> 的 <strong>绝对路径</strong> 是 <code>\"dir/subdir2/subsubdir2/file2.ext\"</code> 。每个目录名由字母、数字和/或空格组成，每个文件名遵循 <code>name.extension</code> 的格式，其中<meta charset=\"UTF-8\" />&nbsp;<code>name</code>&nbsp;和<meta charset=\"UTF-8\" />&nbsp;<code>extension</code>由字母、数字和/或空格组成。</p>\n",
    "\n",
    "<p>给定一个以上述格式表示文件系统的字符串 <code>input</code> ，返回文件系统中&nbsp;<em>指向&nbsp;<strong>文件</strong>&nbsp;的 <strong>最长绝对路径</strong> 的长度</em>&nbsp;。 如果系统中没有文件，返回&nbsp;<code>0</code>。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2020/08/28/dir1.jpg\" style=\"height: 101px; width: 200px;\" />\n",
    "<pre>\n",
    "<strong>输入：</strong>input = \"dir\\n\\tsubdir1\\n\\tsubdir2\\n\\t\\tfile.ext\"\n",
    "<strong>输出：</strong>20\n",
    "<strong>解释：</strong>只有一个文件，绝对路径为 \"dir/subdir2/file.ext\" ，路径长度 20\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2020/08/28/dir2.jpg\" style=\"height: 151px; width: 300px;\" />\n",
    "<pre>\n",
    "<strong>输入：</strong>input = \"dir\\n\\tsubdir1\\n\\t\\tfile1.ext\\n\\t\\tsubsubdir1\\n\\tsubdir2\\n\\t\\tsubsubdir2\\n\\t\\t\\tfile2.ext\"\n",
    "<strong>输出：</strong>32\n",
    "<strong>解释：</strong>存在两个文件：\n",
    "\"dir/subdir1/file1.ext\" ，路径长度 21\n",
    "\"dir/subdir2/subsubdir2/file2.ext\" ，路径长度 32\n",
    "返回 32 ，因为这是最长的路径</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>input = \"a\"\n",
    "<strong>输出：</strong>0\n",
    "<strong>解释：</strong>不存在任何文件</pre>\n",
    "\n",
    "<p><strong>示例 4：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>input = \"file1.txt\\nfile2.txt\\nlongfile.txt\"\n",
    "<strong>输出：</strong>12\n",
    "<strong>解释：</strong>根目录下有 3 个文件。\n",
    "因为根目录中任何东西的绝对路径只是名称本身，所以答案是 \"longfile.txt\" ，路径长度为 12\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= input.length &lt;= 10<sup>4</sup></code></li>\n",
    "\t<li><code>input</code> 可能包含小写或大写的英文字母，一个换行符 <code>'\\n'</code>，一个制表符 <code>'\\t'</code>，一个点 <code>'.'</code>，一个空格 <code>' '</code>，和数字。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [longest-absolute-file-path](https://leetcode.cn/problems/longest-absolute-file-path/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [longest-absolute-file-path](https://leetcode.cn/problems/longest-absolute-file-path/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"dir\\\\n\\\\tsubdir1\\\\n\\\\tsubdir2\\\\n\\\\t\\\\tfile.ext\"', '\"dir\\\\n\\\\tsubdir1\\\\n\\\\t\\\\tfile1.ext\\\\n\\\\t\\\\tsubsubdir1\\\\n\\\\tsubdir2\\\\n\\\\t\\\\tsubsubdir2\\\\n\\\\t\\\\t\\\\tfile2.ext\"', '\"a\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        ans = 5\n",
    "        depths = [0] \n",
    "        lines = ['root']\n",
    "        for line in input.split('\\n'):\n",
    "            x = 0 \n",
    "            while line[x] == '\\t':\n",
    "                x+=1\n",
    "            depths.append(x+1) \n",
    "            lines.append(line.strip('\\t'))\n",
    "\n",
    "        def isfilename(s):\n",
    "            if '.' not in s:\n",
    "                return False\n",
    "            x = s.index('.')\n",
    "            if 0 < x < len(s) - 1:\n",
    "                return True \n",
    "            else:\n",
    "                return False \n",
    "\n",
    "        n = len(lines) \n",
    "        def dfs(i, path):\n",
    "            nonlocal ans \n",
    "            s = lines[i]\n",
    "            if s == '':\n",
    "                return\n",
    "            # print(path + \"/\" + s) \n",
    "            if isfilename(s):\n",
    "                ans = max(len(path) + len(s), ans) \n",
    "                return \n",
    "            for j in range(i+1, n):\n",
    "                if depths[j] == depths[i]+1:\n",
    "                    dfs(j, path+'/'+s) \n",
    "                elif depths[j] <= depths[i]:\n",
    "                    break \n",
    "        dfs(0, '')\n",
    "        return ans - 5 \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def lengthLongestPath(self, input: str) -> int:\r\n",
    "        lines = input.split('\\n')\r\n",
    "        \r\n",
    "        files = []\r\n",
    "\r\n",
    "        cwd = []\r\n",
    "        for line in lines:\r\n",
    "            depth = line.count('\\t')\r\n",
    "            name = line.replace('\\t', '')\r\n",
    "\r\n",
    "            if '.' not in name:\r\n",
    "                cwd = cwd[:depth] + [name]\r\n",
    "            else:\r\n",
    "                files.append(cwd[:depth] + [name])\r\n",
    "\r\n",
    "\r\n",
    "        print(files)\r\n",
    "\r\n",
    "        longest = 0\r\n",
    "\r\n",
    "        for f in files:\r\n",
    "            longest = max(longest, len('/'.join(f)))\r\n",
    "\r\n",
    "        return longest\r\n",
    "\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        i = 0\n",
    "        res = 0\n",
    "        d = {}\n",
    "        while i<len(input):\n",
    "            depth = 0\n",
    "            if input[i]==\"\\n\":\n",
    "                i+=1\n",
    "            while i<len(input) and input[i]==\"\\t\":\n",
    "                depth+=1\n",
    "                i+=1\n",
    "            j=i\n",
    "            is_dir = True\n",
    "            while j<len(input) and input[j]!=\"\\n\":\n",
    "                if input[j]==\".\":\n",
    "                    is_dir=False\n",
    "                j+=1\n",
    "            cur_level_path = input[i:j]\n",
    "            pre_level_path = d[depth-1] if depth-1 in d else \"\"\n",
    "            path = pre_level_path+\"/\"+cur_level_path if pre_level_path != \"\" else cur_level_path\n",
    "            if is_dir:\n",
    "                d[depth] = path\n",
    "            else:\n",
    "                res = max(res,len(path))\n",
    "            i=j+1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        st = []\n",
    "        ans, i, n = 0, 0, len(input)\n",
    "        while i<n:\n",
    "            depth=1\n",
    "            while i<n and input[i]=='\\t':\n",
    "                depth += 1\n",
    "                i += 1\n",
    "            \n",
    "            length, isFile = 0 , False\n",
    "            while i < n and input[i] != '\\n':\n",
    "                if input[i] == '.':\n",
    "                    isFile = True\n",
    "                length += 1\n",
    "                i += 1\n",
    "            i+=1\n",
    "\n",
    "            while len(st) >= depth:\n",
    "                st.pop()\n",
    "            if st:\n",
    "                length += st[-1]+1\n",
    "            if isFile:\n",
    "                ans = max(ans, length)\n",
    "            else:\n",
    "                st.append(length)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        stack = []\n",
    "        maxlen = 0\n",
    "\n",
    "        for part in input.split('\\n'):\n",
    "            level = part.count('\\t')\n",
    "            while level < len(stack):\n",
    "                stack.pop()\n",
    "            stack.append(part[level:])\n",
    "\n",
    "            if '.' in part:\n",
    "                sum_ = sum(len(s) for s in stack)\n",
    "\n",
    "                sum_ += len(stack) - 1\n",
    "                maxlen = max(maxlen, sum_)\n",
    "        return maxlen            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        res = 0\n",
    "        depth_length_map = {-1: 0}\n",
    "        for line in input.split('\\n'):\n",
    "            depth = line.count('\\t')\n",
    "            # 每行 \\t 空格最后要被去掉\n",
    "            depth_length_map[depth] = depth_length_map[depth - 1] + len(line) - depth\n",
    "            if line.count('.'):\n",
    "                res = max(res, depth_length_map[depth] + depth)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        lines = input.split(\"\\n\")\n",
    "        depth_map = {-1:0}\n",
    "        ans = 0;\n",
    "        for line in lines:\n",
    "            depth = line.count('\\t')\n",
    "            depth_map[depth] = depth_map[depth-1] + len(line) - depth\n",
    "            if(line.count('.')):\n",
    "                ans = max(ans, depth_map[depth] + depth)\n",
    "\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        list1 = []\n",
    "        input = input.split(\"\\n\")\n",
    "        max_len = 0\n",
    "        for item in input:\n",
    "            index = item.count('\\t')\n",
    "            list1[index:] = [item.replace('\\t','')]\n",
    "            if '.' in item:\n",
    "                length = len('/'.join(list1))\n",
    "                max_len = max(max_len,length)\n",
    "\n",
    "        return max_len"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        o = 0\n",
    "        stack, curr = [], 0\n",
    "        tag, depth, isfile = 0, 0, False\n",
    "        for s in input:\n",
    "            if s == \"\\n\":\n",
    "                while stack and stack[-1][1]>=depth:\n",
    "                    curr -= stack[-1][0]\n",
    "                    stack.pop()\n",
    "                if isfile:\n",
    "                    o = max(o, curr+tag)\n",
    "                    isfile = False\n",
    "                    tag, depth = 0, 0\n",
    "                    continue\n",
    "                tag += 1\n",
    "                curr += tag\n",
    "                stack.append((tag,depth))\n",
    "                tag, depth = 0, 0\n",
    "            elif s == \"\\t\":\n",
    "                depth += 1\n",
    "            else:\n",
    "                tag += 1\n",
    "                if s == \".\":\n",
    "                    isfile = True\n",
    "        if isfile:\n",
    "            while stack and stack[-1][1]>=depth:\n",
    "                curr -= stack[-1][0]\n",
    "                stack.pop()\n",
    "            o = max(o, curr+tag)\n",
    "        return o"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        res= 0\n",
    "        depth_len_map= {-1: 0}\n",
    "        for line in input.split('\\n'):\n",
    "            depth= line.count('\\t')\n",
    "            depth_len_map[depth]= depth_len_map[depth -1] + len(line) - depth\n",
    "            if line.count('.'):\n",
    "                res= max(res, depth_len_map[depth]+ depth)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        \"\"\"\n",
    "        :type input: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        stack = [0] # initializes the stack\n",
    "        max_len = 0 # initializes the max length\n",
    "        # splits all \\n into an element in a list\n",
    "        for s in input.split('\\n'):\n",
    "            #counts the numbers of \\t inorder to take into account the \"\\\" in the max_length calculation\n",
    "            level = s.count('\\t')\n",
    "            #pops the stack when you go to a new directory (not tab related)\n",
    "            while len(stack) > level + 1:\n",
    "                stack.pop()\n",
    "            # calculates the length of current directory and adds to stack\n",
    "            length = stack[-1] + len(s) - level + 1\n",
    "            stack.append(length)\n",
    "            # if a period is detected, that is a file and thus a dead end, therefore update max length\n",
    "            if '.' in s:\n",
    "                max_len = max(max_len, length - 1)\n",
    "        return max_len"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        inputs = input.split(\"\\n\")\n",
    "        result = 0\n",
    "        st = []\n",
    "        for f in inputs:\n",
    "            tcnt = len(f.split('\\t')) - 1\n",
    "            file = f.split('\\t')[-1]\n",
    "            while(len(st) > tcnt):\n",
    "                st.pop()\n",
    "            st.append(file)\n",
    "\n",
    "            if('.' in file):\n",
    "                result = max(result,len('/'.join(st)))\n",
    "        return result\n",
    "            \n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        \"\"\"\n",
    "        :type input: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        stack = [0] # initializes the stack\n",
    "        max_len = 0 # initializes the max length\n",
    "\n",
    "        # splits all \\n into an element in a list\n",
    "        for s in input.split('\\n'):\n",
    "\n",
    "            #counts the numbers of \\t inorder to take into account the \"\\\" in the max_length calculation\n",
    "            level = s.count('\\t')\n",
    "\n",
    "            #pops the stack when you go to a new directory (not tab related)\n",
    "            while len(stack) > level + 1:\n",
    "                stack.pop()\n",
    "\n",
    "            # calculates the length of current directory and adds to stack\n",
    "            length = stack[-1] + len(s) - level + 1\n",
    "            stack.append(length)\n",
    "\n",
    "            # if a period is detected, that is a file and thus a dead end, therefore update max length\n",
    "            if '.' in s:\n",
    "                max_len = max(max_len, length - 1)\n",
    "\n",
    "        return max_len"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        ls = input.split('\\n')\n",
    "        foo, ans = [], 0\n",
    "        for s in ls:\n",
    "            d = s.count('\\t')\n",
    "            while len(foo) > d:\n",
    "                foo.pop()\n",
    "            foo.append(len(s) - d )\n",
    "            if '.' in s:\n",
    "                ans = max(ans, sum(foo) + len(foo) - 1)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        sub_strs = input.split('\\n')\n",
    "        d_map = {-1: 0}\n",
    "        max_len = 0\n",
    "        for item in sub_strs:\n",
    "            depth = item.count('\\t')\n",
    "            content = item[depth*2:]\n",
    "            d_map[depth] = d_map[depth-1] + len(item) - depth\n",
    "            if '.' in item:\n",
    "                max_len = max(max_len, d_map[depth] + depth)\n",
    "        return max_len\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def __init__(self):\n",
    "        self.max_len = 0\n",
    "        self.idx = 0\n",
    "        self.cur_len = 0\n",
    "        self.path = None\n",
    "\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        self.path = input.split('\\n')\n",
    "        self.parse(0)\n",
    "        return self.max_len\n",
    "\n",
    "    def parse(self, depth):\n",
    "        while self.idx < len(self.path) and self.get_depth() == depth:\n",
    "            this_len = 1 + len(self.path[self.idx]) - depth\n",
    "            self.cur_len += this_len\n",
    "            self.idx += 1\n",
    "            if '.' in self.path[self.idx - 1]:\n",
    "                self.max_len = max(self.max_len, self.cur_len - 1)\n",
    "            else:\n",
    "                self.parse(depth + 1)\n",
    "            self.cur_len -= this_len\n",
    "\n",
    "    def get_depth(self):\n",
    "        st = self.path[self.idx]\n",
    "        depth = 0\n",
    "        while depth < len(st) and st[depth] == '\\t':\n",
    "            depth += 1\n",
    "        return depth"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        res = 0\n",
    "        depth_length_map = {-1: 0}\n",
    "        for line in input.split('\\n'):\n",
    "            depth = line.count('\\t')\n",
    "            # 每行空格最后要被去掉  \\t 的个数\n",
    "            depth_length_map[depth] = depth_length_map[depth - 1] + len(line) - depth\n",
    "            if line.count('.'):\n",
    "                # 每层都要添加depth个 / \n",
    "                res = max(res, depth_length_map[depth] + depth)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        dirStack = []\n",
    "        maxLen = 0\n",
    "\n",
    "        for d in input.split('\\n'):\n",
    "            temp = d.split('\\t')\n",
    "            depth = len(temp)\n",
    "            dirName = temp[-1]\n",
    "\n",
    "            if depth > len(dirStack):\n",
    "                dirStack.append(dirName)\n",
    "            elif depth == len(dirStack):\n",
    "                dirStack[-1] = dirName\n",
    "            else:\n",
    "                while depth < len(dirStack):\n",
    "                    dirStack.pop()\n",
    "                dirStack[-1] = dirName\n",
    "\n",
    "            if dirName.count('.'):  # is file\n",
    "                maxLen = max(maxLen, len('/'.join(dirStack)))\n",
    "\n",
    "        return maxLen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "\n",
    "        def isFile(dir: str) -> bool: \n",
    "            for ch in dir:\n",
    "                if ch == '.':\n",
    "                    return True\n",
    "            return False\n",
    "\n",
    "        def toLengthAndLevel(dir: str) -> Tuple[int, int]: \n",
    "            level = 0\n",
    "            for ch in dir: \n",
    "                if ch == '\\t':\n",
    "                    level += 1\n",
    "                else:\n",
    "                    break\n",
    "            return (level, len(dir)-level)\n",
    "\n",
    "\n",
    "        dirs = input.split('\\n')\n",
    "\n",
    "        stack = []\n",
    "\n",
    "        res = 0\n",
    "\n",
    "        for dir in dirs: \n",
    "            isfile = isFile(dir)\n",
    "            level, dirLen = toLengthAndLevel(dir)\n",
    "            \n",
    "            while stack and level <= stack[-1][0]:\n",
    "                stack.pop()\n",
    "            \n",
    "            currDirLen = stack[-1][1] if stack else 0\n",
    "\n",
    "            if isfile:\n",
    "                fileLen = currDirLen + len(dir)\n",
    "                res = max(res, fileLen)\n",
    "            else:\n",
    "                dirLen += currDirLen\n",
    "                stack.append((level, dirLen))\n",
    "        \n",
    "        return res\n",
    "\n",
    "\n",
    "            \n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        i_list = input.split('\\n')\n",
    "        pn = []\n",
    "        ln = []\n",
    "        tn=0\n",
    "        ret = 0\n",
    "        for i in i_list:\n",
    "            si = i.split('\\t')\n",
    "            if len(si)>tn:\n",
    "                tn = len(si)\n",
    "                pn.append(si[-1])\n",
    "                ln.append(len(si[-1]))\n",
    "            else:\n",
    "                pop_num = tn-len(si)+1\n",
    "                while pop_num>0:\n",
    "                    pn.pop()\n",
    "                    ln.pop()\n",
    "                    pop_num-=1\n",
    "                pn.append(si[-1])\n",
    "                ln.append(len(si[-1]))\n",
    "                tn = len(si)\n",
    "            if '.' in si[-1]:\n",
    "                temp = 0\n",
    "                for sret in ln:\n",
    "                    temp+=sret\n",
    "                    temp+=1\n",
    "                temp-=1\n",
    "                if temp>ret:\n",
    "                    ret=temp \n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        l = list(input.split('\\n'))\n",
    "        res = 0\n",
    "        d_dic = {-1:0}\n",
    "        for i in l:\n",
    "            depth = i.count('\\t')\n",
    "            d_dic[depth] = d_dic[depth-1]+len(i)-depth\n",
    "            if i.count('.'):\n",
    "                res = max(res,d_dic[depth]+depth)\n",
    "        return res\n",
    "\n",
    "                    \n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        l = input.split('\\n')\n",
    "        result = 0\n",
    "        father_len = 0\n",
    "        father_level = -1\n",
    "        for i in range(len(l)):\n",
    "            counter = 0\n",
    "            while l[i][counter] == '\\t':\n",
    "                counter += 1\n",
    "            if counter == 0:\n",
    "                path_len = len(l[i])\n",
    "            else:\n",
    "                for j in range(i,-1,-1):\n",
    "                    if l[j][0] == counter-1:\n",
    "                        break\n",
    "                path_len = l[j][1]+len(l[i])-counter+1\n",
    "            if '.' in l[i]:\n",
    "                result = max(result,path_len)\n",
    "            l[i] = [counter,path_len]\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        \n",
    "        if '.' not in input:\n",
    "            return 0\n",
    "        arr = input.split('\\n')\n",
    "        # print(arr)\n",
    "        n = len(arr)\n",
    "        stack, ans = [], 0\n",
    "        for i in range(n):\n",
    "            while stack and arr[i].count('\\t') < len(stack):\n",
    "                stack.pop()\n",
    "            stack.append(arr[i].replace('\\t', ''))\n",
    "            if  \".\" in stack[-1]:\n",
    "                ans = max(ans, len(\"\".join(stack)) + len(stack) - 1)\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        \n",
    "        n = len(input)\n",
    "        ans, i, st = 0, 0, []\n",
    "        \n",
    "        while i < n:\n",
    "            depth = 1\n",
    "            while i < n and input[i] == \"\\t\":\n",
    "                i += 1\n",
    "                depth += 1\n",
    "            \n",
    "            length = 0\n",
    "            isFile = False\n",
    "            while i < n and input[i] != \"\\n\":\n",
    "                length += 1\n",
    "                if input[i] == \".\":\n",
    "                    isFile = True\n",
    "                i += 1\n",
    "            \n",
    "            i += 1\n",
    "            while len(st) >= depth:\n",
    "                st.pop()\n",
    "            if st:\n",
    "                length += st[-1] + 1\n",
    "            if isFile:\n",
    "                ans = max(ans, length)\n",
    "            else:\n",
    "                st.append(length)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "      res = 0\n",
    "      depth_map = {-1: 0}\n",
    "      for line in input.split('\\n'):\n",
    "        depth = line.count('\\t')\n",
    "        depth_map[depth] = depth_map[depth-1] + len(line) - depth\n",
    "        if line.count('.'):\n",
    "          res = max(res, depth_map[depth] + depth)\n",
    "      return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        if not input:\n",
    "            return 0\n",
    "        strList = input.replace(\"\\t\", \"/\").split('\\n')\n",
    "        maxLen, stack = 0, []\n",
    "        for i in range(len(strList)):\n",
    "            deep, lenN = 0, len(strList[i])\n",
    "            for j in range(0, lenN):\n",
    "                if strList[i][j] == '/':\n",
    "                    deep += 1\n",
    "                else:\n",
    "                    break\n",
    "            while len(stack) > deep:\n",
    "                stack.pop()\n",
    "            stack.append(strList[i][deep:lenN])\n",
    "            flag = False\n",
    "            for j in range(lenN-1, -1, -1):\n",
    "                if strList[i][j] == '.':\n",
    "                    flag = True\n",
    "                    break\n",
    "            if flag:\n",
    "                temp = \"/\".join(stack)\n",
    "                maxLen = max(maxLen, len(temp))\n",
    "        return maxLen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        stk = []\n",
    "        idx = 0\n",
    "        ans = 0\n",
    "        n = len(input)\n",
    "        while idx < n:\n",
    "            depth = 1\n",
    "            while input[idx] == '\\t':\n",
    "                depth += 1\n",
    "                idx += 1\n",
    "            length = 0\n",
    "            is_file = False\n",
    "            while idx < n and input[idx] != '\\n':\n",
    "                length += 1\n",
    "                if input[idx] == '.': is_file = True\n",
    "                idx += 1\n",
    "            idx += 1\n",
    "            while len(stk) >= depth:\n",
    "                stk.pop()\n",
    "            if stk: length += stk[-1] + 1\n",
    "            if is_file: ans = max(ans, length)\n",
    "            else: stk.append(length)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:        \n",
    "        path = input.split('\\n')\n",
    "        ans = 0\n",
    "        depth_len_dict = {-1: 0}\n",
    "        for p in path:\n",
    "            depth = p.count('\\t')\n",
    "\n",
    "            # 上一层加上当前的长度，当前长度=p - '\\t'个数\n",
    "            depth_len_dict[depth] = depth_len_dict[depth - 1] + len(p) - depth\n",
    "            if '.' in p:\n",
    "                ans = max(ans, depth_len_dict[depth] + depth)\n",
    "\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        st = []\n",
    "        ans, i, n = 0, 0, len(input)\n",
    "        while i < n:\n",
    "            # 检测当前文件的深度\n",
    "            depth = 1\n",
    "            while i < n and input[i] == '\\t':\n",
    "                depth += 1\n",
    "                i += 1\n",
    "\n",
    "            # 统计当前文件名的长度\n",
    "            length, isFile = 0, False\n",
    "            while i < n and input[i] != '\\n':\n",
    "                if input[i] == '.':\n",
    "                    isFile = True\n",
    "                length += 1\n",
    "                i += 1\n",
    "            i += 1  # 跳过换行符\n",
    "\n",
    "            while len(st) >= depth:\n",
    "                st.pop()\n",
    "            if st:\n",
    "                length += st[-1] + 1\n",
    "            if isFile:\n",
    "                ans = max(ans, length)\n",
    "            else:\n",
    "                st.append(length)\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        arr = input.split(\"\\n\")\n",
    "        n = len(arr)\n",
    "        res, cur = 0, 0\n",
    "        stk = []\n",
    "        for dirs in arr:\n",
    "            level = 0\n",
    "            while len(dirs) > 0 and dirs[0] == '\\t': \n",
    "                level += 1\n",
    "                dirs = dirs[1:]\n",
    "            if len(stk) == 0:\n",
    "                stk.append([len(dirs), level])\n",
    "                cur = len(dirs)\n",
    "            else:\n",
    "                if level > stk[-1][1]:\n",
    "                    cur += 1 + len(dirs)\n",
    "                    stk.append([1 + len(dirs), level])\n",
    "                else:\n",
    "                    while len(stk) > 0 and level <= stk[-1][1]:\n",
    "                        cur -= stk[-1][0] \n",
    "                        stk.pop()\n",
    "                    dirLen = len(dirs)\n",
    "                    if len(stk) > 0: dirLen += 1\n",
    "                    cur += dirLen\n",
    "                    stk.append([dirLen, level])\n",
    "            if \".\" in dirs: res = max(res, cur)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        res = 0\n",
    "        depth_length_map = {-1:0}\n",
    "        for line in input.split('\\n'):\n",
    "            depth = line.count('\\t')\n",
    "            depth_length_map[depth] = depth_length_map[depth - 1] + len(line) - depth\n",
    "            if line.count('.'):\n",
    "                res = max(res, depth_length_map[depth] + depth)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        result=0\n",
    "        cur_stack=[]\n",
    "        ss=input.split('\\n')\n",
    "        for s in ss:\n",
    "            l=0\n",
    "            while s[0]=='\\t':\n",
    "                l+=1\n",
    "                s=s[1:]\n",
    "            if s.find('.')>=0:\n",
    "                r=len(s)+sum(cur_stack[:l])+l\n",
    "                if r>result:\n",
    "                    result=r\n",
    "            else:\n",
    "                if l==len(cur_stack):\n",
    "                    cur_stack.append(len(s))\n",
    "                else:\n",
    "                    cur_stack[l]=len(s)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calc(self, pd, lev):\n",
    "        l = 0\n",
    "        if \".\" not in pd[lev]:\n",
    "            return l\n",
    "\n",
    "        for i in range(1, lev + 1):\n",
    "            l += len(pd[i]) + 1\n",
    "        return l - 1\n",
    "\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "\n",
    "        paths = input.split(\"\\n\")\n",
    "        pd = dict()\n",
    "\n",
    "        if len(paths) <= 1:\n",
    "            pd[1] = paths[0]\n",
    "            return self.calc(pd, 1)\n",
    "\n",
    "        pd[1] = paths[0]\n",
    "        \n",
    "        res = 0\n",
    "\n",
    "        for i in range(1, len(paths)):\n",
    "            cur = paths[i].split(\"\\t\")\n",
    "            lev = len(cur)\n",
    "            \n",
    "            pd[lev] = cur[-1]\n",
    "            l = self.calc(pd, lev)\n",
    "            res = max(l, res)\n",
    "        \n",
    "        return res\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        r = []\n",
    "        st = 0\n",
    "        def dfs(st,pre,n):\n",
    "            pos = \"\"\n",
    "            while st < len(input) and input[st] != \"\\n\":\n",
    "                pos+=input[st]\n",
    "                st += 1\n",
    "            if \".\" in pos:\n",
    "                s = \"\"\n",
    "                for i in range(min(len(pre),n)):\n",
    "                    s += pre[i]\n",
    "                    s += \"/\"\n",
    "                r.append(s+pos)\n",
    "                dfs(st,pre,n)\n",
    "            else:\n",
    "                if st == len(input):\n",
    "                    return \n",
    "                if input[st:].startswith(\"\\n\"):\n",
    "                    st += 1\n",
    "                i = 0\n",
    "                while input[st:].startswith(\"\\t\"):\n",
    "                    st+=1\n",
    "                    i+=1\n",
    "                if i > n:\n",
    "                    if i-1 >= len(pre):\n",
    "                        pre.append(pos)\n",
    "                    else:\n",
    "                        pre[i-1] = pos\n",
    "                        pre = pre[:i]\n",
    "                dfs(st,pre,i)\n",
    "        dfs(0,[],0)\n",
    "        mi = 0\n",
    "        print(r)\n",
    "        for i in r:\n",
    "            mi = max(mi,len(i))\n",
    "        return mi\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        '''\n",
    "        看了题解\n",
    "        depth_length_map 作为前缀和\n",
    "        '''\n",
    "        res = 0\n",
    "        depth_length_map = {-1: 0}\n",
    "        for line in input.split('\\n'):\n",
    "            depth = line.count('\\t')\n",
    "            # -depth是去掉每行\\t空格\n",
    "            depth_length_map[depth] = depth_length_map[depth-1] + len(line) - depth\n",
    "            # 判断是否有文件\n",
    "            if line.count('.'):\n",
    "                # 每层都要添加depth个/\n",
    "                res = max(res, depth_length_map[depth]+depth)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        res = 0\n",
    "        depth_length_map = defaultdict(int)\n",
    "        # depth_length_map = {-1: 0}\n",
    "        for line in input.split('\\n'):\n",
    "            depth = line.count('\\t')\n",
    "            #每行空格最后要被去掉\n",
    "            depth_length_map[depth] = depth_length_map[depth - 1] + len(line) - depth\n",
    "            if line.count('.'):\n",
    "                # 每层都要添加depth个 / \n",
    "                res = max(res, depth_length_map[depth] + depth)\n",
    "        return res\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        # 堆栈\n",
    "        ss = []\n",
    "        lines = input.split('\\n')\n",
    "        n = len(lines)\n",
    "        i = 0\n",
    "        res = 0\n",
    "        while i<n:\n",
    "            items = lines[i].split('\\t')\n",
    "            c = len(items)\n",
    "            while ss and ss[-1][1]>=c:\n",
    "                ss.pop(-1)\n",
    "            ss.append([items[-1], c])\n",
    "            if '.' in lines[i]:\n",
    "                tmp = '/'.join([i[0] for i in ss])\n",
    "                res = max(res, len(tmp))\n",
    "            i += 1\n",
    "        return res\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        res = 0\n",
    "        depth_length_map = {-1: 0}\n",
    "        for line in input.split('\\n'):\n",
    "            depth = line.count('\\t')\n",
    "            # 每行 \\t 空格最后要被去掉\n",
    "            depth_length_map[depth] = depth_length_map[depth - 1] + len(line) - depth\n",
    "            if line.count('.'):\n",
    "                # 每层都要添加depth个 / \n",
    "                res = max(res, depth_length_map[depth] + depth)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "\n",
    "        pathrc = []\n",
    "\n",
    "        inputlist = input.split('\\n')\n",
    "\n",
    "        maxlenth = 0\n",
    "        \n",
    "\n",
    "        for ins in inputlist:\n",
    "\n",
    "            cts = 0\n",
    "\n",
    "            # ctsp = 0\n",
    "\n",
    "            while ins[cts] == '\\t':\n",
    "                cts += 1\n",
    "                # ctsp += 1\n",
    "            \n",
    "            \n",
    "            while cts < len(pathrc):\n",
    "                pathrc.pop()\n",
    "\n",
    "            \n",
    "            # if cts == len(pathrc):\n",
    "            pathrc.append(ins[cts:])\n",
    "\n",
    "            # print(pathrc, len(\"/\".join(pathrc)))\n",
    "            # if ins[-4] == '.':\n",
    "            if '.' in ins:\n",
    "\n",
    "                if len(\"/\".join(pathrc)) > maxlenth:\n",
    "                    maxlenth = len(\"/\".join(pathrc))\n",
    "                \n",
    "            # while \n",
    "        return maxlenth\n",
    "\n",
    "\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        paths=[]\n",
    "        directories=input.split('\\n')\n",
    "        ans=0\n",
    "        for filename in directories:\n",
    "            count=0\n",
    "            idx=0\n",
    "            while idx<len(filename) and filename[idx]=='\\t':\n",
    "                count+=1\n",
    "                idx+=1\n",
    "            while len(paths)>count:\n",
    "                paths.pop()\n",
    "            name=filename[idx:]\n",
    "            paths.append(name)\n",
    "            if '.' in name:\n",
    "                # 是一个文件名而非目录名\n",
    "                ans=max(ans,len('/'.join(paths)))\n",
    "        return ans\n",
    "\n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        ans,i,n = 0,0,len(input)\n",
    "        level = [0]*(n+1)\n",
    "        while i<n:\n",
    "            depth = 1\n",
    "            while i<n and input[i]=='\\t':\n",
    "                depth+=1\n",
    "                i+=1\n",
    "            length, isFile = 0, False\n",
    "            while i<n and input[i] != '\\n':\n",
    "                if input[i] == '.':\n",
    "                    isFile = True\n",
    "                length+=1\n",
    "                i+=1\n",
    "            i+=1\n",
    "            if depth > 1:\n",
    "                length += level[depth-1] + 1\n",
    "            if isFile:\n",
    "                ans = max(ans, length)\n",
    "            else:\n",
    "                level[depth] = length\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        ans = 0\n",
    "        i, n = 0, len(input)\n",
    "\n",
    "        st = []\n",
    "\n",
    "        while i < n:\n",
    "            depth = 1\n",
    "            while i < n and input[i] == '\\t':\n",
    "                depth += 1\n",
    "                i += 1\n",
    "\n",
    "            while len(st) >= depth:\n",
    "                st.pop()\n",
    "\n",
    "            length = 0\n",
    "            isFile = False\n",
    "            while i < n and input[i] != '\\n':\n",
    "                if input[i] == \".\":\n",
    "                    isFile = True\n",
    "                i += 1\n",
    "                length += 1\n",
    "\n",
    "            if st:\n",
    "                length += st[-1] + 1\n",
    "            if isFile:\n",
    "                ans = max(ans, length)\n",
    "            else:\n",
    "                st.append(length)\n",
    "            i += 1\n",
    "\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        st = []\n",
    "        ans,i,n = 0,0,len(input)\n",
    "        while i < n:\n",
    "            depth = 1\n",
    "            while i < n and input[i] == '\\t':\n",
    "                depth += 1\n",
    "                i += 1\n",
    "            \n",
    "            length , isFile = 0,False\n",
    "            while i < n and input[i] != '\\n':\n",
    "                if input[i] == '.':\n",
    "                    isFile = True\n",
    "                \n",
    "                length += 1\n",
    "                i += 1 \n",
    "            i += 1\n",
    "\n",
    "            while len(st) >= depth:\n",
    "                st.pop()\n",
    "            \n",
    "            if st:\n",
    "                length += st[-1] + 1\n",
    "            \n",
    "            if isFile:\n",
    "                ans = max(ans,length)\n",
    "            else:\n",
    "                st.append(length)\n",
    "        return ans\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        st = []\n",
    "        ans, i, n = 0, 0, len(input)\n",
    "        while i < n:\n",
    "            # 检测当前文件的深度\n",
    "            depth = 1\n",
    "            while i < n and input[i] == '\\t':\n",
    "                depth += 1\n",
    "                i += 1\n",
    "\n",
    "            # 统计当前文件名的长度\n",
    "            length, isFile = 0, False\n",
    "            while i < n and input[i] != '\\n':\n",
    "                if input[i] == '.':\n",
    "                    isFile = True\n",
    "                length += 1\n",
    "                i += 1\n",
    "            i += 1  # 跳过换行符\n",
    "\n",
    "            while len(st) >= depth:\n",
    "                st.pop()\n",
    "            if st:\n",
    "                length += st[-1] + 1\n",
    "            if isFile:\n",
    "                ans = max(ans, length)\n",
    "            else:\n",
    "                st.append(length)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        ans, i, n = 0, 0, len(input)\n",
    "        level = [0] * (n + 1)\n",
    "        while i < n:\n",
    "            # 检测当前文件的深度\n",
    "            depth = 1\n",
    "            while i < n and input[i] == '\\t':\n",
    "                depth += 1\n",
    "                i += 1\n",
    "\n",
    "            # 统计当前文件名的长度\n",
    "            length, isFile = 0, False\n",
    "            while i < n and input[i] != '\\n':\n",
    "                if input[i] == '.':\n",
    "                    isFile = True\n",
    "                length += 1\n",
    "                i += 1\n",
    "            i += 1  # 跳过换行符\n",
    "\n",
    "            if depth > 1:\n",
    "                length += level[depth - 1] + 1\n",
    "            if isFile:\n",
    "                ans = max(ans, length)\n",
    "            else:\n",
    "                level[depth] = length\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        sub_strs = input.split('\\n')\n",
    "        items, indents = [], []\n",
    "        for i in range(len(sub_strs)):\n",
    "            num = sub_strs[i].count('\\t')\n",
    "            start = num * len('\\t')\n",
    "            content = sub_strs[i][start:]\n",
    "            items.append(content)\n",
    "            indents.append(num)\n",
    "        \n",
    "        print(indents)\n",
    "        print(items)\n",
    "\n",
    "        max_len = len(items[0]) if '.' in items[0] else 0\n",
    "        path = [items[0]]\n",
    "        indexs = [0]\n",
    "        for i in range(1, len(items)):\n",
    "            while len(path) > 0 and indents[i] <= indents[indexs[-1]]:\n",
    "                indexs.pop()\n",
    "                path.pop()\n",
    "            if len(path) <= 0 or indents[i] > indents[indexs[-1]]:\n",
    "                path.append(items[i])\n",
    "                indexs.append(i)\n",
    "                if '.' in path[-1]:\n",
    "                    max_len = max(max_len, len('/'.join(path)))\n",
    "        return max_len\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        stack = []\n",
    "        input = input.split('\\n')\n",
    "        ans = 0\n",
    "        for t in input:\n",
    "            level = 0\n",
    "            for ch in t:\n",
    "                if ch != '\\t':\n",
    "                    break\n",
    "                level += 1\n",
    "            while len(stack) > level:\n",
    "                stack.pop()\n",
    "            if '.' in t:\n",
    "                ans = max(ans, (stack[-1] if stack else 0) + len(t) - level)\n",
    "            else:\n",
    "                stack.append((stack[-1] if stack else 0) + len(t) - level + 1)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        if '.' not in input:\n",
    "            return 0\n",
    "\n",
    "        locs = input.split('\\n')\n",
    "        stack = []\n",
    "        res = 0\n",
    "        level = -1\n",
    "\n",
    "        for loc in locs:\n",
    "            nt = loc.count('\\t')\n",
    "            while nt <= level:\n",
    "                stack.pop()\n",
    "                level -= 1\n",
    "            level = nt\n",
    "            stack.append(loc[nt:])\n",
    "            if '.' in loc:\n",
    "                # print('/'.join(stack))\n",
    "                res = max(res, len('/'.join(stack)))\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        inputs = input.split(\"\\n\")\n",
    "        result = 0\n",
    "        st = []\n",
    "        for f in inputs:\n",
    "            tcnt = len(f.split('\\t')) - 1\n",
    "            file = f.split('\\t')[-1]\n",
    "            while(len(st) > tcnt):\n",
    "                st.pop()\n",
    "            st.append(file)\n",
    "            if('.' in file):\n",
    "                result = max(result,len('/'.join(st)))\n",
    "        return result\n",
    "            \n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        def getDepth(dir_name):\n",
    "            return len(dir_name.split('\\t'))-1\n",
    "        def getName(dir_name):\n",
    "            tmp_list = dir_name.split('\\t')\n",
    "            return tmp_list[-1]\n",
    "        def isFile(dir_name):\n",
    "            return '.'in dir_name\n",
    "        if '.' not in input:\n",
    "            return 0\n",
    "        dir_list = input.split('\\n')\n",
    "        root = dir_list[0]\n",
    "        path_stack = []\n",
    "        path_stack.append(root)\n",
    "        longest_len = len(root)\n",
    "        len_tmp = len(root)\n",
    "        depth = 0\n",
    "        for item in dir_list[1:]:\n",
    "            depth_tmp = getDepth(item)\n",
    "            if depth_tmp > depth:\n",
    "                depth = depth_tmp\n",
    "            elif depth_tmp == depth:\n",
    "                parallel_dir = path_stack.pop()\n",
    "                len_tmp -= (len(parallel_dir)+1)\n",
    "            else:\n",
    "                for i in range(depth,depth_tmp-1,-1):\n",
    "                    len_tmp -= (len(path_stack.pop()) + 1)\n",
    "                depth = depth_tmp\n",
    "            dir_name = getName(item)\n",
    "            path_stack.append(dir_name)\n",
    "            len_tmp += (len(dir_name)+1)\n",
    "            if isFile(dir_name):\n",
    "                if len_tmp > longest_len:\n",
    "                    longest_len = len_tmp\n",
    "        return longest_len"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, s: str) -> int:\n",
    "        stk = []\n",
    "        ans = 0\n",
    "        for line in s.split('\\n'):\n",
    "            name = line.lstrip('\\t')\n",
    "            level = len(line) - len(name)\n",
    "            while len(stk) > level:\n",
    "                stk.pop()\n",
    "            stk.append(name)\n",
    "            if '.' in name:\n",
    "                s = '/'.join(stk)\n",
    "                print(s)\n",
    "                ans = max(ans, len(s))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        inputs = input.split(\"\\n\")\n",
    "        result = 0\n",
    "        st = []\n",
    "        for f in inputs:\n",
    "            tcnt = len(f.split('\\t')) - 1\n",
    "            file = f.split('\\t')[-1]\n",
    "            while(len(st) > tcnt):\n",
    "                st.pop()\n",
    "            st.append(file)\n",
    "            if('.' in file):\n",
    "                result = max(result,len('/'.join(st)))\n",
    "        return result\n",
    "            \n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        s = []\n",
    "        ans = 0\n",
    "        path = input.split('\\n')\n",
    "        for p in path:\n",
    "            start,layer = 0,0\n",
    "            while start + 1 < len(p):\n",
    "                #print(p[start:start + 2])\n",
    "                if p[start:start + 1] != '\\t':\n",
    "                    break\n",
    "                start += 1\n",
    "                layer += 1\n",
    "            while len(s) != 0 and s[len(s) - 1][0] >= layer:\n",
    "                 s.pop()\n",
    "\n",
    "            dir = p[start:]\n",
    "            s.append((layer,dir))\n",
    "            if dir.find('.') != -1:\n",
    "                filelen = 0\n",
    "                for dir in s:\n",
    "                    filelen += len(dir[1]) + 1\n",
    "                ans = max(filelen -1,ans)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "#\n",
    "# @lc app=leetcode.cn id=388 lang=python\n",
    "#\n",
    "# [388] 文件的最长绝对路径\n",
    "#\n",
    "\n",
    "# @lc code=start\n",
    "class Solution(object):\n",
    "    def lengthLongestPath(self, input):\n",
    "        \"\"\"\n",
    "        :type input: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        paths = input.split('\\n')\n",
    "        max_len = 0\n",
    "        stack = [0]\n",
    "        pre = 0\n",
    "        for path in paths:\n",
    "            depth = path.count('\\t')\n",
    "            while depth < len(stack) - 1:\n",
    "                stack.pop()\n",
    "            pre = stack[-1]\n",
    "            stack.append(pre + len(path) - depth)\n",
    "            if depth != 0:\n",
    "                stack[-1] += 1\n",
    "            #print(stack)\n",
    "            if '.' in path:\n",
    "                max_len = max(max_len, stack[-1])\n",
    "\n",
    "        return max_len\n",
    "# @lc code=end\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        mp = defaultdict(int)\n",
    "        ans = 0\n",
    "        for line in input.split('\\n'):\n",
    "            d = line.count('\\t')\n",
    "            mp[d] = mp[d - 1] + len(line) - d\n",
    "            if '.' in line:\n",
    "                ans = max(ans, mp[d] + d)\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        \"\"\"\n",
    "        :type input: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        stack = [0] # initializes the stack\n",
    "        max_len = 0 # initializes the max length\n",
    "        # splits all \\n into an element in a list\n",
    "        for s in input.split('\\n'):\n",
    "            #counts the numbers of \\t inorder to take into account the \"\\\" in the max_length calculation\n",
    "            level = s.count('\\t')\n",
    "            #pops the stack when you go to a new directory (not tab related)\n",
    "            while len(stack) > level + 1:\n",
    "                stack.pop()\n",
    "            # calculates the length of current directory and adds to stack\n",
    "            length = stack[-1] + len(s) - level + 1\n",
    "            stack.append(length)\n",
    "            # if a period is detected, that is a file and thus a dead end, therefore update max length\n",
    "            if '.' in s:\n",
    "                max_len = max(max_len, length - 1)\n",
    "        return max_len"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        s=list(input.split(\"\\n\"))\n",
    "        res=[]\n",
    "        dic_1={}\n",
    "        # print(s)\n",
    "\n",
    "        for a in s:\n",
    "            i=1\n",
    "            while \"\\t\" in a:\n",
    "                \n",
    "                if a[0:1]==\"\\t\":\n",
    "                    i+=1\n",
    "                    a=a[1:]\n",
    "\n",
    "            dic_1[a]=i\n",
    "        # temp=[]\n",
    "        # for x,y in dic_1.items():\n",
    "        #     temp.append([y,x])\n",
    "\n",
    "        # print(temp[::-1])\n",
    "        print(dic_1)\n",
    "        stack=[[\"\",0]]\n",
    "        maxl=0\n",
    "        for dir,num in dic_1.items():\n",
    "            if num>stack[-1][1]:\n",
    "                stack.append([dir,num])\n",
    "                if \".\" in dir:\n",
    "                    strl=\"\"\n",
    "                    for x in stack:\n",
    "                        strl = strl+x[0]+r\"/\"\n",
    "                    \n",
    "                    strl=strl[1:-1]\n",
    "                    print(strl)\n",
    "                    print(len(strl))\n",
    "                    maxl=max(maxl,len(strl))\n",
    "            elif num==stack[-1][1]:\n",
    "                stack.pop()\n",
    "                stack.append([dir,num])\n",
    "                if \".\" in dir:\n",
    "                    strl=\"\"\n",
    "                    for x in stack:\n",
    "                        strl = strl+x[0]+r\"/\"\n",
    "                    \n",
    "                    strl=strl[1:-1]\n",
    "                    print(strl)\n",
    "                    print(len(strl))\n",
    "                    maxl=max(maxl,len(strl))\n",
    "            elif num<stack[-1][1]:\n",
    "                while stack[-1][1]-num>=0:\n",
    "                    stack.pop()\n",
    "\n",
    "                stack.append([dir,num])\n",
    "                if \".\" in dir:\n",
    "                    strl=\"\"\n",
    "                    for x in stack:\n",
    "                        strl = strl+x[0]+r\"/\"\n",
    "                    \n",
    "                    strl=strl[1:-1]\n",
    "                    print(strl)\n",
    "                    print(len(strl))\n",
    "                    maxl=max(maxl,len(strl))\n",
    "        \n",
    "        print(maxl)\n",
    "        return maxl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        sub_strs = input.split('\\n')\n",
    "        items, indents = [], []\n",
    "        for i in range(len(sub_strs)):\n",
    "            num = sub_strs[i].count('\\t')\n",
    "            start = num * len('\\t')\n",
    "            content = sub_strs[i][start:]\n",
    "            items.append(content)\n",
    "            indents.append(num)\n",
    "        \n",
    "        max_len = len(items[0]) if '.' in items[0] else 0\n",
    "        path = [items[0]]\n",
    "        indexs = [0]\n",
    "        for i in range(1, len(items)):\n",
    "            while len(path) > 0 and indents[i] <= indents[indexs[-1]]:\n",
    "                indexs.pop()\n",
    "                path.pop()\n",
    "            if len(path) <= 0 or indents[i] > indents[indexs[-1]]:\n",
    "                path.append(items[i])\n",
    "                indexs.append(i)\n",
    "                if '.' in path[-1]:\n",
    "                    max_len = max(max_len, len('/'.join(path)))\n",
    "        return max_len\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def lengthLongestPath(self, input: str) -> int:\r\n",
    "        ans, i, n = 0, 0, len(input)\r\n",
    "        level = [0] * (n + 1)\r\n",
    "        while i < n:\r\n",
    "            # 检测当前文件的深度\r\n",
    "            depth = 1\r\n",
    "            while i < n and input[i] == '\\t':\r\n",
    "                depth += 1\r\n",
    "                i += 1\r\n",
    "\r\n",
    "            # 统计当前文件名的长度\r\n",
    "            length, isFile = 0, False\r\n",
    "            while i < n and input[i] != '\\n':\r\n",
    "                if input[i] == '.':\r\n",
    "                    isFile = True\r\n",
    "                length += 1\r\n",
    "                i += 1\r\n",
    "            i += 1  # 跳过换行符\r\n",
    "\r\n",
    "            if depth > 1:\r\n",
    "                length += level[depth - 1] + 1\r\n",
    "            if isFile:\r\n",
    "                ans = max(ans, length)\r\n",
    "            else:\r\n",
    "                level[depth] = length\r\n",
    "        return ans\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        res = 0\n",
    "        depth_length_map = {-1: 0}\n",
    "        for line in input.split('\\n'):  # 每行\n",
    "            depth = line.count('\\t')\n",
    "            # 每行空格最后要被去掉  \\t 的个数\n",
    "            depth_length_map[depth] = depth_length_map[depth - 1] + len(line) - depth  # 去除前面的符号\n",
    "            if line.count('.'):  # 文件\n",
    "                # 每层都要添加depth个 / \n",
    "                res = max(res, depth_length_map[depth] + depth)  # 最后的长度\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        st = []\n",
    "        ans, i, n = 0, 0, len(input)\n",
    "        while i < n:\n",
    "            # 检测当前文件的深度\n",
    "            depth = 1\n",
    "            while i < n and input[i] == '\\t':\n",
    "                depth += 1\n",
    "                i += 1\n",
    "\n",
    "            # 统计当前文件名的长度\n",
    "            length, isFile = 0, False\n",
    "            while i < n and input[i] != '\\n':\n",
    "                if input[i] == '.':\n",
    "                    isFile = True\n",
    "                length += 1\n",
    "                i += 1\n",
    "            i += 1  # 跳过换行符\n",
    "\n",
    "            while len(st) >= depth:\n",
    "                st.pop()\n",
    "            if st:\n",
    "                length += st[-1] + 1\n",
    "            if isFile:\n",
    "                ans = max(ans, length)\n",
    "            else:\n",
    "                st.append(length)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        inputs = input.split(\"\\n\")\n",
    "        result = 0\n",
    "        st = []\n",
    "        for f in inputs:\n",
    "            tcnt = len(f.split('\\t')) - 1\n",
    "            file = f.split('\\t')[-1]\n",
    "            while(len(st) > tcnt):\n",
    "                st.pop()\n",
    "            st.append(file)\n",
    "            if('.' in file):\n",
    "                result = max(result,len('/'.join(st)))\n",
    "        return result\n",
    "            \n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        elements = input.split(\"\\n\")\n",
    "        dirs = [\"\"]\n",
    "        lastdegree = 0\n",
    "        curlen = 0\n",
    "        ans = 0\n",
    "        def getdegree(name):\n",
    "            i = 0\n",
    "            deg = 0\n",
    "            while i + 2 <= len(name) and name[i:i + 1] == \"\\t\":\n",
    "                deg += 1\n",
    "                i += 1\n",
    "            return deg, name[i:]\n",
    "\n",
    "        def filecheck(name):\n",
    "            return \".\" in name\n",
    "        \n",
    "        for filefolder in elements:\n",
    "            current_degree, trimmed = getdegree(filefolder)\n",
    "            isfile = filecheck(trimmed)\n",
    "            for _ in range(lastdegree - current_degree + 1):\n",
    "                temp = dirs.pop()\n",
    "                curlen -= len(temp)\n",
    "            curlen += len(trimmed)\n",
    "            dirs.append(trimmed)\n",
    "            if isfile:\n",
    "                ans = max(ans, curlen + current_degree)\n",
    "            lastdegree = current_degree\n",
    "        \n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, input: str) -> int:\n",
    "        \n",
    "        if '.' not in input:\n",
    "            return 0\n",
    "        arr = input.split('\\n')\n",
    "        print(arr)\n",
    "        n = len(arr)\n",
    "        stack, ans = [], 0\n",
    "        for i in range(n):\n",
    "            while stack and arr[i].count('\\t') < len(stack):\n",
    "                stack.pop()\n",
    "            stack.append(arr[i].replace('\\t', ''))\n",
    "            if  \".\" in stack[-1]:\n",
    "                ans = max(ans, len(\"\".join(stack)) + len(stack) - 1)\n",
    "            # print(stack, ans)\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def lengthLongestPath(self, input: str) -> int:\r\n",
    "        paths = input.split('\\n')\r\n",
    "        stack = []\r\n",
    "        cur, ans = 0, 0\r\n",
    "        for path in paths:\r\n",
    "            cur_level = path.count('\\t')\r\n",
    "            pre_level = len(stack) - 1\r\n",
    "            while cur_level <= pre_level:\r\n",
    "                pre_level -= 1\r\n",
    "                cur -= stack.pop()\r\n",
    "            stack.append(len(path) - path.count('\\t'))\r\n",
    "            cur += stack[-1]\r\n",
    "            if path.find('.') != -1:\r\n",
    "                ans = max(ans, cur + len(stack) - 1)\r\n",
    "        return ans\r\n",
    "        \r\n",
    "\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def lengthLongestPath(self, s: str) -> int:\n",
    "        def length(n):\n",
    "            return (n >> 8) & 0xffff\n",
    "        def leaf(n):\n",
    "            return (n >> 24) & 0xff\n",
    "        def lv(n):\n",
    "            return n & 0xff\n",
    "        def Node(s):\n",
    "            i, n = 0, 0\n",
    "            lv, len, leaf = 0, 0, 0\n",
    "            while s[i] != '\\n':\n",
    "                if s[i] == '\\t':\n",
    "                    lv += 1\n",
    "                else:\n",
    "                    len += 1\n",
    "                    if s[i] == '.':\n",
    "                        leaf = 1\n",
    "                i += 1\n",
    "            n |= lv & 0xff\n",
    "            n |= (len & 0xffff) << 8\n",
    "            n |= (leaf & 0xff) << 24\n",
    "            return n\n",
    "\n",
    "        ans, stack = 0, []\n",
    "        s += '\\n'\n",
    "        k = 0\n",
    "        for i in range(len(s)):\n",
    "            if s[i] == '\\n':\n",
    "                n = Node(s[k:]) \n",
    "                while (len(stack) > 0 and\n",
    "                    lv(n) <= lv(stack[-1])):\n",
    "                    stack.pop()\n",
    "                if leaf(n):\n",
    "                    stack.append(n)\n",
    "                    cnt = 0\n",
    "                    for m in range(len(stack)):\n",
    "                        cnt += length(stack[m])\n",
    "                    cnt += lv(stack[-1])\n",
    "                    ans = ans if ans > cnt else cnt\n",
    "                    stack.pop()\n",
    "                else:\n",
    "                    if (len(stack) <= 0 or\n",
    "                        lv(stack[-1]) < lv(n)):\n",
    "                        stack.append(n)\n",
    "                k = i + 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def lengthLongestPath(self, input: str) -> int:\r\n",
    "        lines = input.split('\\n')\r\n",
    "        \r\n",
    "        files = []\r\n",
    "\r\n",
    "        cwd = []\r\n",
    "        for line in lines:\r\n",
    "            depth = line.count('\\t')\r\n",
    "            name = line.replace('\\t', '')\r\n",
    "\r\n",
    "            if '.' not in name:\r\n",
    "                cwd = cwd[:depth] + [name]\r\n",
    "            else:\r\n",
    "                files.append(cwd[:depth] + [name])\r\n",
    "\r\n",
    "\r\n",
    "        longest = 0\r\n",
    "\r\n",
    "        for f in files:\r\n",
    "            longest = max(longest, len('/'.join(f)))\r\n",
    "\r\n",
    "        return longest\r\n",
    "\r\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
